var app = getApp()
Page({
    data: {
        order: {},
        btnDesc: '',
        needPay: true,
        openType: '',
        payType: 0
    },
    onLoad: function (options) {
        new app.ToastPannel();
        var that = this;
        /** 
         * 获取系统信息 
         */
        swan.getSystemInfo({
            success: function (res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }
        });

        const order = JSON.parse(options.order);
        let needPay = true, btnDesc='';
        if(order.status == 0) {
            btnDesc = `一键付款(￥${order.amout})`
        }
        else {
            needPay = false;
            btnDesc = order.payDesc;
        }
        this.setData({
            needPay: needPay,
            btnDesc: btnDesc,
            order: JSON.parse(options.order),
            openType: app.globalData.config.payType === 1 ? 'contact' : '',
            payType: app.globalData.config.payType
        });
    },
    payOrder: function () {
        let _this = this;
        let url = `${app.globalData.prefix}/api/orders/${_this.data.order._id}/payment`;
        swan.request({
            url: url,
            header: {  
                token: app.globalData.token,
                channel: app.globalData.channel || ''
            },
            method: 'GET',
            success: function(res) {
                console.log(res);
                if(!res.data.status) {
                    swan.showToast(
                        {
                          title: res.data.msg,
                          duration: 1200
                        }
                      );
                    return;
                }
                
                if(_this.data.payType === 1) {//个人
                    //
                    console.log('personal pay')
                }
                else if (_this.data.payType === 2) {//企业
                    swan.requestPayment({
                        timeStamp: res.data.data.timeStamp,
                        nonceStr: res.data.data.nonceStr,
                        package: res.data.data.package,
                        signType: res.data.data.signType,
                        paySign: res.data.data.paySign,
                        success:function(res){
                            // console.log(res);
                            swan.switchTab({
                                url: '../orders/orders?tab=1'
                            });
                        },
                        fail:function(res){
                            // console.log(res);
                            if(res.errMsg !== 'requestPayment:fail cancel') {
                                swan.showToast(
                                    {
                                      title: `支付失败, ${res.errMsg}`,
                                      duration: 1200
                                    }
                                  );
                            }
                            /*
                            swan.switchTab({
                                url: '../orders/orders'
                            });
                            */
                        },
                        complete:function(res){}
                    });
                }
                else {
                    swan.navigateTo({
                        url: `../webview/webview?orderid=${res.data.data.orderCode}`
                    })
                }
            },
            fail: function(err) {
                console.log(err);
            }
        });
    }
});